package com.wantupai.translate.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.wantupai.translate.Constant;
import com.wantupai.translate.R;
import com.wantupai.translate.base.BaseActivity;
import com.wantupai.translate.bean.DataBean;
import com.wantupai.translate.bean.EventBean;
import com.wantupai.translate.bean.PhoneLoginBean;
import com.wantupai.translate.utils.CommonUtils;
import com.wantupai.translate.utils.NetUtils;
import com.wantupai.translate.utils.NetworkUtils;
import com.wantupai.translate.utils.PrefUtils;
import com.wantupai.translate.utils.ToastUtils;
import com.wantupai.translate.utils.WindowUtils;
import com.wantupai.translate.widget.popuwindow.LoginPopuWindow;
import com.wantupai.translate.widget.roundedimageview.RoundedImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author luxuchang
 * @date 2019/4/29 0029
 * @description
 */
public class PersonalInfoActivity extends BaseActivity {
    @BindView(R.id.head_view)
    RoundedImageView headView;
    @BindView(R.id.nick_name)
    TextView nickName;
    private LoginPopuWindow loginPop;

    @Override
    public int getLayoutId() {
        return R.layout.activity_personal_info;
    }

    @Override
    public void initViews() {
        EventBus.getDefault().register(this);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setTitle("个人中心");

        loginPop = new LoginPopuWindow(this, new LoginPopuWindow.OnBtnClickListener() {
            @Override
            public void OnBtnClick(int type,String phoneNum,String codeNum) {
                switch (type) {
                    case 0:
                        sendSms(phoneNum);
                        break;
                    case 1:
                        phoneLogin(phoneNum,codeNum);
                        break;
                    case 2:
                        ToastUtils.show("查看协议");
                        break;
                    case 3:
                        ToastUtils.show("查看隐私政策");
                        break;
                }
            }
        });
    }

    @Subscribe
    public void onEventMainThread(EventBean bean) {
        loginPop.showCodeImage();
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void processClick(View v) {

    }

    @OnClick({R.id.head_view, R.id.rl_card_view, R.id.rl_history, R.id.rl_about_us, R.id.rl_xieyi, R.id.rl_more_app})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_view:
                loginPop.show();
                break;
            case R.id.rl_card_view:
                break;
            case R.id.rl_history:
                startActivity(new Intent(PersonalInfoActivity.this, MyIdentificationActivity.class));
                break;
            case R.id.rl_about_us:
                startActivity(new Intent(PersonalInfoActivity.this, AboutUsActivity.class));
                break;
            case R.id.rl_xieyi:
                break;
            case R.id.rl_more_app:
                startActivity(new Intent(PersonalInfoActivity.this, MoreAppActivity.class));
                break;
        }
    }


    private void phoneLogin(String phoneNum, String code) {
        Map<String, String> maps = new HashMap<>();
        maps.put("mobile", phoneNum);
        maps.put("code", code);
        maps.put("device_id", WindowUtils.getDeviceId(this));
        String model = WindowUtils.getSystemModel();
        maps.put("phone_model", TextUtils.isEmpty(model) ? "Android" : model);
        maps.put("phone_system", "Android");
        maps.put("jp_id", "");
        int NetType = NetworkUtils.getNetWorkType(this);
        if (NetType == 0) {
            Toast.makeText(this, "没有连接网络", Toast.LENGTH_SHORT).show();
        } else {
            NetUtils.getInstance().post(Constant.SSP_API_PHONE_LOGIN, maps, false, PhoneLoginBean.class, 0, new NetUtils.NetResponseListener() {
                @Override
                public void onSuccess(DataBean result, int requestCode) {
                    try {
                        PhoneLoginBean bean = (PhoneLoginBean) result.getData();
                        PrefUtils.setString(Constant.TPUID, bean.getUid());
                        ToastUtils.show(result.getInfo());
                        loginPop.dismiss();
                    } catch (Exception e) {
                        ToastUtils.show("登录失败，请重新登录");
                    }

                }

                @Override
                public void onFailed(int requestCode, String info) {
                    ToastUtils.show(info);
                }

            });
        }
    }

    private void sendSms(final String phoneNum) {
        Map<String, String> maps = new HashMap<>();
        maps.put("phone", phoneNum);
        int NetType = NetworkUtils.getNetWorkType(this);
        if (NetType == 0) {
            Toast.makeText(this, "没有连接网络", Toast.LENGTH_SHORT).show();
        } else {
            NetUtils.getInstance().post(Constant.SSP_API_SEND_SMS, maps, false, null, 0, new NetUtils.NetResponseListener() {
                @Override
                public void onSuccess(DataBean result, int requestCode) {
                    ToastUtils.show("发送成功");
                    loginPop.startTimer();
                }

                @Override
                public void onFailed(int requestCode, String info) {
                    ToastUtils.show(info);
                }
            });
        }

    }


}
